import csv
import pprint
from datetime import datetime

from matplotlib import pyplot as plt

# 从文件中获取时间，最低气温，最高气温
filename = 'resource/sitka_weather_2014.csv'
with open(filename) as f:
    reader = csv.reader(f)
    header_row = next(reader)

    # for index, column_header in enumerate(header_row): # 用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列，同时列出数据和数据下标
    #     print(index, column_header)

    dates, highs, lows = [], [], []
    for row in reader:
        try:
            current_date = datetime.strptime(row[0], "%Y-%m-%d")
            high = int(row[1])
            low = int(row[3])
        except ValueError:
            print(current_date, 'missing data')
        else:
            dates.append(current_date)
            highs.append(high)
            lows.append(low)

print(dates)
print(highs)
print(lows)

# Plot data.
fig = plt.figure(dpi=128, figsize=(10, 6))
plt.plot(dates, highs, c='red', alpha=0.5)  # alpha指定颜色透明度，0为完全透明
plt.plot(dates, lows, c='blue', alpha=0.5)
plt.fill_between(dates, highs, lows, facecolor='black', alpha=0.1)
'''
我们向fill_between() 传递了一个 x 值系列:列表dates ，
还传递了两个 y 值系列:highs 和lows 。
实参facecolor 指定了填充区域的颜色
'''

# Format plot.
title = "Daily high and low temperatures - 2014\nDeath Valley, CA"
plt.title(title, fontsize=20)
plt.xlabel('', fontsize=16)
fig.autofmt_xdate()  # 绘制斜的日期标签
plt.ylabel("Temperature (F)", fontsize=16)
plt.tick_params(axis='both', which='major', labelsize=16)

# plt.show()
plt.savefig("figures/Daily_high_low_temperatures001.png", bbox_inches='tight')  # 自动保存图表
